﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace Grundy.Common
{
    public static class ExtensionMethods
    {
        public static void ForEachItemDo<T>(this IEnumerable<T> enumerable, Action<T> action) where T : class 
        {
            var enumerator = enumerable.GetEnumerator();
            while (enumerator.MoveNext())
            {
                if (enumerator.Current != null)
                {
                    action(enumerator.Current);
                }
            }
        }

        public static string Serialize(this object o)
        {
            var serializer = JsonSerializer.Create(new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All });
            serializer.Converters.Add(new StringEnumConverter());

            var sb = new StringBuilder();
            serializer.Serialize(new StringWriter(sb), o);
            return sb.ToString();
        }

        public static T Deserialize<T>(this string s)
        {
            var serializer = JsonSerializer.Create(new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All });
            serializer.Converters.Add(new StringEnumConverter());

            T o = (T) serializer.Deserialize(new StringReader(s), typeof (T));
            
            return o;
        }

        public static bool IsNullOrEmpty<T>(this IEnumerable<T> e)
        {
            return e == null || e.Count() == 0;
        }

        public static string ToHexadecimalString(this byte[] ba)
        {
            string hex = BitConverter.ToString(ba);
            return hex.Replace("-", "");
        }

        public static byte[] ToByteArray(this string hex)
        {
            if (string.IsNullOrWhiteSpace(hex))
            {
                return new byte[0];
            }
            int numberChars = hex.Length;
            byte[] bytes = new byte[numberChars / 2];
            for (int i = 0; i < numberChars; i += 2)
                bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
            return bytes;
        }

        public static byte[] ToByteArrayUsingUtf8(this string s)
        {   
            return UTF8Encoding.UTF8.GetBytes(s);
        }

        public static bool EqualsIgnoreCase(this string a, string b)
        {
            return string.Compare(a, b, StringComparison.InvariantCultureIgnoreCase) == 0;
        }
        
        public static string FormatWith(this string format, params object[] args)
        {
            return string.Format(format, args);
        }

        public static bool ContainsIgnoreCase(this IEnumerable<string> collection, string s)
        {
            return collection.Contains(s, StringComparer.InvariantCultureIgnoreCase);
        }
    }
}
